<?php
namespace app\wechat\controller;

use app\wechat\model\Wechat;
use think\Controller;

class Test extends Controller
{
    public function index()
    {
        $echoStr = isset($_GET['echostr']) ? $_GET['echostr'] : '';
        $wechat = new Wechat();
        if ($wechat->checkSignature() && $echoStr) {
            return $echoStr;
        } else {
            $postObj = $wechat->sdkInit();
            if(strtolower($postObj->MsgType) == 'event' && strtolower($postObj->Event) == 'subscribe'){
                $this->checkFocus();
            }
            $this->checkClickEvent();
//            $wechat->responseGraphicMsg($postObj,$wechat->wechatHotSpider('农旅',0 , 'wechatAllHot'));
        }
    }

    public function clear()
    {
        $wechat = new Wechat();
        $cache = $wechat->memcacheInit();
        $cache->clear('curl');
        return 'true';

    }

    public function getToken(){
        $wechat = new Wechat();
        return $wechat->getAccessToken('curl','access_token');
    }
   
    //发送模板消息
    public function send_tempmate_message($data){
        $wechat = new Wechat();
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wechat->getAccessToken('curl' , 'access_token');
        $res = $this->http_request($url , $data);
        return json_decode($res , true);
    }
    //https请求（支持GET和POST）
    protected function http_request($url , $data = null){
        $curl - curl_init();
        curl_setopt($curl , CURLOPT_URL , $url);
        curl_setopt($curl , CURLOPT_SSL_VERIFYPEER , false);
        curl_setopt($curl , CURLOPT_SSL_VERIFYHOST , false);
        if(!empty($data)){ 
            curl_setopt($curl , CURLOPT_POST , 1);
            curl_setopt($curl , CURLOPT_POSTFIELDS , $data);
        }
        curl_setopt($curl , CURLOPT_RETURNTRANSFER , 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    //获取用户列表
    public function get_user_list(){
        $wechat = new Wechat();
        
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+$wechat->getAccessToken('curl','access_token');
        $res = $this->http_request($url);
        return dump($res);
    }
    // public send_message(){
    //     $template=array(
    //             'touser' => "",
    //             'template_id' => "",
    //             'url' => "",
    //             'topcolor' => '#7B68BB',
    //             'data' => array(
    //                     'first' =>
    //                 )     
    //         );
    //     return var_dump($this->send_tempmate_message(urldecode(json_encode($template))))
    // }
}


